package com.wtj.user.controller;

import com.wtj.user.dto.UserPageReq;
import com.wtj.user.dto.UserPageResp;
import com.wtj.user.dto.UserReq;
import com.wtj.user.service.UserService;
import com.wtj.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 注：引入Mybatis-Plus 要在启动类上配置@MapperScan("com.wtj.user.mapper")
 *    引入Mybatis 要在配置类上配置xml文件的位置
 * mybatis-plus:
 *   mapper-locations: classpath:/com/wtj/user/mapper/*.xml
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     *  演示 mybatis-plus 的使用
     *  获取用户列表   curl http://localhost:8080/user/page?pageNum=1&pageSize=1
     * @param userPageReq
     * @return
     */
    @GetMapping("/page")
    public ResponseResult<List<UserPageResp>> getUserList(UserPageReq userPageReq) {
        List<UserPageResp> userPage = userService.getUserPage(userPageReq);
        return new ResponseResult<>(200,userPage);
    }

    /**
     *  演示 mybatis 的使用
     *  获取用户信息   curl http://localhost:8080/user/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult<UserPageResp> getUserById(@PathVariable("id") Long id)  {
        UserPageResp user = userService.getUserById(id);
        return new ResponseResult<>(200,user);
    }

    /**
     * 演示 mybatis 的使用
     *  登录   curl -X POST http://localhost:8080/user/login -H "Content-Type: application/json" -d '{"username":"admin","password":"123456"}'
     * @param userReq
     * @return
     */
    @PostMapping("/login")
    public ResponseResult<Map<String, String>> login(@RequestBody UserReq userReq) {
        Map<String, String> login = userService.login(userReq);
        return new ResponseResult<>(200,"登录成功",login);
    }

    /**
     * 演示 mybatis 的使用
     *  注册   curl -X POST http://localhost:8080/user/register -H "Content-Type: application/json" -d '{"username":"admin","password":"123456"}'
     * @param userReq
     * @return
     */
    @PostMapping("/register")
    public ResponseResult<String> register(@RequestBody UserReq userReq) {
        userService.register(userReq);
        return new ResponseResult<>(200, "注册成功");
    }

    /**
     * 演示 mybatis 的使用
     *  删除用户信息   curl -X POST http://localhost:8080/user/delete/1
     * @param
     * @return
     */
    @PostMapping("/logout")
    public ResponseResult<String> loginOut() {
        userService.loginOut();
        return new ResponseResult<>(200, "退出成功");
    }
}
